/**
 * 提示与加载工具类
 */
export default class Tips {
    static isLoading = false;
    static pause = false;

    static GetFnPromise(fn, obj = {}) {
        return new Promise((resolve, reject) => {
            obj.success = function(res) { resolve(res) }
            obj.fail = function(res) { reject(res) }
            fn(obj);
        });
    }

    /**
     * 弹出提示框
     */

    static success(title, duration = 1500) {
        let potions = {
            title: title,
            icon: 'success',
            mask: true,
            duration: duration
        }
        return this.GetFnPromise(wx.showToast, potions);
    }

    /**
     * 纯文字提示框
     */

    static text(title, duration = 2000) {
        const subTitle = title.substr(0, 40);
        let options = {
            title: subTitle,
            icon: 'none',
            mask: true,
            duration: duration
        };
        return this.GetFnPromise(wx.showToast, options);
    }

    /**
     * 错误框
     */

    static error(msg) {
        return this.text(msg);
    }

    /**
     * 弹出确认窗口
     */
    static alert(text, title = '提示', confirmText = '确定') {
        let opt = {
            title: title,
            content: text,
            showCancel: false,
            confirmText: confirmText
        }
        return this.GetFnPromise(wx.showModal, opt);
    }

    /**
     * 弹出确认窗口
     */
    static confirm(text, title = '确认提示') {
        let opt = {
            title: title,
            content: text,
            showCancel: true,
        }
        return this.GetFnPromise(wx.showModal, opt);
    }

    static confirmBtn(text, title, confirmText, cancelText) {
        let opt = {
            title: title,
            confirmText: confirmText,
            cancelText: cancelText,
            cancelColor: "#000000",
            confirmColor: "#576B95",
            content: text,
            showCancel: true,
        }
        return this.GetFnPromise(wx.showModal, opt);
    }


    /**
     * 弹出加载提示
     */
    static loading(title = '') {
        if (this.isLoading) {
            return;
        }
        this.isLoading = true;
        if (wx.showLoading) {
            wx.showLoading({
                title: title,
                mask: true
            });
        } else {
            wx.showNavigationBarLoading();
        }
    }

    /**
     * 加载完毕
     */
    static loaded() {
        if (this.isLoading) {
            this.isLoading = false;
            if (wx.hideLoading) {
                wx.hideLoading();
            } else {
                wx.hideNavigationBarLoading();
            }
        }
    }

    static setLoading() {
        this.isLoading = true;
    }
}